<?php
include_once ("../Login/databaseconnection.php");
	$Resource_Id=$_POST['Resource_Id'];
	$New_Resource=$_POST['New_Resource'];
if($New_Resource!=""&&$Resource_Id!="Select One")
{
$_SESSION['error']="Please select the resource or type new!";
}
else
{
   date_default_timezone_set("Europe/Paris");
   $t_d=date("Y-m-d");
if($New_Resource!="")
{
$maxid=mysql_query("SELECT max(Resource_Id) as maxId FROM resource");

while($rowid=mysql_fetch_array($maxid))
{
$m_Id=$rowid['maxId'];
$inum=substr("$m_Id",1);
$inn=($inum+1);
$Resource_Id='R';
if(strlen($inn)<strlen($inum))
{
for($i=0;$i<(strlen($inum)-strlen($inn));$i++)
{
$Resource_Id=$Resource_Id.'0';
}
$Resource_Id=$Resource_Id.$inn;
}
else
{
$Resource_Id=$Resource_Id.$inn;
}
}
$sql_res=mysql_query("INSERT INTO resource(Resource_Id,Resource_Type,Last_Updated_Date)values('$Resource_Id','$New_Resource','$t_d')");
}
	$Lab_Id=$_POST['Lab_Id'];
	$Serial_Number=$_POST['Serial_Number'];
	$Functioning=$_POST['Functioning'];
    $Brand_Name=$_POST['Brand_Name'];
    $Date_Of_Supplied=$_POST['Date_Of_Supplied'];
    $Name_Of_Supplier=$_POST['Name_Of_Supplier'];
    $Funding_Source=$_POST['Funding_Source'];
	$SchName=$_POST['SchName'];
	$name=explode(",",$SchName);
    $nam=$name[0];
$tn=count($name);
for($i=1;$i<$tn;$i++)
{
if(empty($adr))
{
$adr=$name[$i];
}
else
{
$adr=$adr.",".$name[$i];
}
}
$results= mysql_query("SELECT * FROM school Where Name='$nam' AND Address='$adr'");
if($row = mysql_fetch_array($results))
{
 $School_Id=$row['School_Id'];
}
	date_default_timezone_set("Europe/Paris");
    $t_d=date("Y-m-d");

if(($Resource_Id!="Select One"||$New_Resource!="")&&$Lab_Id!="Select One"&&$Serial_Number!=""&&$Functioning!=""&&$Brand_Name!=""&&$SchName!="")
	{
$sql=mysql_query("INSERT INTO resource_details(Resource_Id,Lab_Id,Serial_Number,Functioning,Brand_Name,Date_Of_Supplied,Name_Of_Supplier,Funding_Source,School_Id,Last_Updated_Date)values('$Resource_Id','$Lab_Id','$Serial_Number','$Functioning','$Brand_Name','$Date_Of_Supplied','$Name_Of_Supplier','$Funding_Source','$School_Id','$t_d')");
if($sql)
  {
$_SESSION['error']="This record is successfully registered!";
$_SESSION['OK']="OK";
$_SESSION['Resource_Id']=$Resource_Id;
$_SESSION['Serial_Number']=$Serial_Number;
  }

  if(mysql_affected_rows()==0)
  {
$_SESSION['error']="Sorry unable to register this record!";
  }
  }
  if(($Resource_Id=="Select One"&&$New_Resource=="")||$Lab_Id=="Select One"||$Serial_Number==""||$Functioning==""||$Brand_Name==""||$SchName=="")
	{
	$_SESSION['errorin']="Indexed details are required,Please fill all!";
  }
}
?>

